﻿using DAL;
using lBLL;
using lDAL;
using Model;

namespace BLL
{
    public class UserBLL: lUserBLL
    {
        public UserBLL(lUserDAL<Userinfor> db , lUserDAL<Hoteinfor> dbs)
        {
            Db = db;
            Dbs = dbs;
        }

        public lDAL.lUserDAL<Userinfor> Db { get; }
        public lUserDAL<Hoteinfor> Dbs { get; }

        public async Task<int> AddAsync(Userinfor userinfor)
        {
            userinfor.UserTimes = DateTime.Now;
            var a = Db.GetAllAsync().Any(x=>x.UserName==userinfor.UserName);
            if(a)
            {
                return -1;
            }
            else
            {
                return await Db.AddAsync(userinfor);
            }
            
        }

        public Task<int> DeleteAsync(int id)
        {
            var a = Db.GetAllAsync().FirstOrDefault(x=>x.UserId==id);
            return Db.DeleteAsync(a);
        }

        public dynamic GetAllAsync(string? name , DateTime? time , int? dq)
        {
            var user = Db.GetAllAsync();
            var hote = Dbs.GetAllAsync();
            var list = from a in user
                       join b in hote
                       on a.HoteId equals b.HoteId
                       select new
                       {
                           a.UserId,
                           b.HoteId,
                           a.UserName,
                           b.HoteAddress,
                           a.UserDian,
                           a.UserPhone,
                           a.UserTime,
                           a.UserZuida,
                           a.UserZhuang,
                           a.UserTimes,
                           a.UserKaiye,

                       };
            if(!string.IsNullOrEmpty(name))
            {
                list = list.Where(x=>x.UserName.Contains(name));
            }
            if(time != null)
            {
                list = list.Where(x => x.UserTime==time);
            }
            if(dq > 0)
            {
                list = list.Where(x => x.HoteId == dq);
            }
            return list.ToList();
        }
        public Task<int> UpdateAsync(Userinfor userinfor)
        {
            return Db.UpdateAsync(userinfor);
        }
    }
}
